package com.zhihuishu.treenity.controller.course;


import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhihuishu.micro.bbs.openapi.onlineschool.dto.OnlinePostModelOpenDto;
import com.zhihuishu.micro.course.openapi.course.CourseDataOpenService;
import com.zhihuishu.micro.course.openapi.course.dto.CourseDataOpenDto;
import com.zhihuishu.remote.RemoteException;
import com.zhihuishu.remote.RemoteResult;
import com.zhihuishu.treenity.controller.BaseController;
import com.zhihuishu.treenity.log.LoggerCollectionEnum;
import com.zhihuishu.treenity.service.course.BBSCourseService;

/**
 * 
 * @author shehuaiyang
 * @date 2016年10月28日 上午9:55:41
 * @modifyNote
 * @version 1.0
 */
@Controller
@RequestMapping("/course/thirdStep/courseData")
public class ThirdStepCourseDataController extends BaseController {
	
	
	@Resource
	private BBSCourseService bbsCourseService;
	
	@Resource
	private CourseDataOpenService courseDataOpenService;
     /**
	 * 查询章讨论列表
	 * @Description
	 * @author shehuaiyang
	 * @date 2016年10月18日 下午3:33:15
	 * @modifyNote 
	 * @param courseId
	 * @param chapterId
	 * @return
	 */
    @Override
    protected LoggerCollectionEnum getCollectionEnum() {
        return LoggerCollectionEnum.disscussionCollection;
    }
	@ResponseBody
	@RequestMapping("/create")
	public RemoteResult<CourseDataOpenDto> saveCourseData(final CourseDataOpenDto courseDataOpenDto,final Integer dataId) throws com.zhihuishu.micro.bbs.openapi.remote.RemoteException {
		RemoteResult<CourseDataOpenDto> result = new ResultRequestAndParser<CourseDataOpenDto>("建课业务",
				"zhihuishu-thirdStep-courseData", "保存资料", "saveCourseData") {
			@Override
			public RemoteResult<CourseDataOpenDto> request(Long curUserId) throws RemoteException {
				RemoteResult<CourseDataOpenDto> rs = courseDataOpenService.saveCourseDataOpen(courseDataOpenDto);
				RemoteResult<OnlinePostModelOpenDto> remoteResult = null;
				try {
					remoteResult = bbsCourseService.findModelById(dataId);
				} catch (com.zhihuishu.micro.bbs.openapi.remote.RemoteException e) {
					log.error(e.getMessage());
				}
				Set<Integer> dataIds = remoteResult.getResult().getDataIds();
				dataIds.add(rs.getResult().getId());
				try {
					bbsCourseService.updateModel(remoteResult.getResult());
				} catch (com.zhihuishu.micro.bbs.openapi.remote.RemoteException e) {
					log.error(e.getMessage());
				}
				return rs;
			}
		}.get();
		return result;

}

	/**
	 * 根据资料id删除资料
	 * @Description
	 * @author shehuaiyang
	 * @date 2016年10月24日 下午7:49:29
	 * @modifyNote 
	 * @param postId
	 * @return
	 * @throws com.zhihuishu.micro.bbs.openapi.remote.RemoteException
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public RemoteResult<Void> deleteCourseData(@RequestParam(value="discussionReferId",required=true) final Integer discussionReferId) {
			RemoteResult<Void> result = new ResultRequestAndParser<Void>("建课业务",
					"zhihuishu-thirdStep-courseData", "删除资料", "deleteCourseData") {
				@Override
				public RemoteResult<Void> request(Long curUserId) throws RemoteException {
					RemoteResult<Void> rs = courseDataOpenService.deleteCourseDataOpen(discussionReferId);
					return rs;
				}
			}.get();
			return result;
	
    }
}
